<?php

namespace app\api\server\index;

use app\api\model\circle\CircleApiModel;
use app\api\model\music\MusicApiModel;
use app\api\model\music\MusicDjApiModel;
use app\api\model\music\MusicSelectedPackApiModel;
use app\api\model\music\MusicSkewerApiModel;
use app\api\model\user\UserFollowApiModel;
use app\api\model\UserModel;
use app\api\ServerBase;
use app\logic\single\WechatSingle;

class IndexServer extends ServerBase
{
    protected $request;
    /**
     * @var UserModel
     */
    private $userModel;
    /**
     * @var MusicApiModel
     */
    private $musicModel;
    /**
     * @var MusicDjApiModel
     */
    private $musicDjModel;
    /**
     * @var MusicSelectedPackApiModel
     */
    private $musicSelectedPackModel;
    /**
     * @var MusicSkewerApiModel
     */
    private $musicSkewerModel;
    /**
     * @var CircleApiModel
     */
    private $circleModel;

    function __construct($request)
    {
        $this->request=$request;
        $this->userModel = new UserModel();
        $this->musicModel = new MusicApiModel();
        $this->musicDjModel = new MusicDjApiModel();
        $this->musicSelectedPackModel = new MusicSelectedPackApiModel();
        $this->musicSkewerModel = new MusicSkewerApiModel();
        $this->circleModel = new CircleApiModel();
    }
    function getWriter(){
        $where['is_writer'] = 1;
        $result = $this->userModel->getSelect($where,
            'id,writer_head_img,writer_name,fans_num,introduction',
            'writer_sort desc',
            4
        );
        if(!empty($result) && !empty($this->request->user_id)){
            foreach ($result as &$val){
                $val['is_follow'] = UserFollowApiModel::checkFollow($val['id'],$this->request->user_id);
            }
            unset($val);
        }
        $this->getMessageCode('adminMessage.success.select', 2);
        return $result;
    }

    function topMusic(){
        $where['single_status'] = 1;
        $music = $this->musicModel->getSelect($where,'id,price,title,img,original_price','sale desc,id desc',10);

        $where2['status'] = 1;
        $field[]='id,price,original_price,sale';
        $field[]=['title','img','url'];
        $dj = $this->musicDjModel->getSelect($where2,
            $field,
            'sale desc,id desc',
            10);

        $selectedPack = $this->musicSelectedPackModel->getSelect($where2,
            'id,title,img,price,original_price,sale',
            'sale desc,id desc',
            10);

        $skewer = $this->musicSkewerModel->getSelect($where2,
            'id,title,img,price,original_price,sale',
            'sale desc,id desc',
            10);

        $this->getMessageCode('adminMessage.success.select', 2);
        return [
            'music'=>$music,
            'dj'=>$dj,
            'selected_pack'=>$selectedPack,
            'skewer'=>$skewer
        ];
    }

    function todayMusic(){
        $where['single_status'] = 1;
        $arr=[];
        $is_all = $this->request->param['is_all']??-1;
        if($is_all == 1){
            $limit = 9999;
        }else{
            $limit = 10;
        }
        for($i = 0; $i<3;$i++){
            $start_time =date('Y-m-d',strtotime("-{$i} day"));
            $end_time =date('Y-m-d 23:59:59',strtotime("-{$i} day"));
            $music = $this->musicModel->whereBetweenTime('create_time',$start_time,$end_time)
                ->where('single_status',1)
                ->field('id,price,original_price,sale,title,img,url,play_num')
                ->order('id desc')
                ->limit($limit)
                ->select();
            $dj = $this->musicDjModel->whereBetweenTime('create_time',$start_time,$end_time)
                ->where('status',1)
                ->field('id,price,original_price,sale,title,img,play_num')
                ->order('id desc')
                ->limit($limit)
                ->select();
            $selectedPack = $this->musicSelectedPackModel->whereBetweenTime('create_time',$start_time,$end_time)
                ->where('status',1)
                ->field('id,price,original_price,sale,title,img,play_num')
                ->order('id desc')
                ->limit($limit)
                ->select();
            $skewer = $this->musicSkewerModel->whereBetweenTime('create_time',$start_time,$end_time)
                ->where('status',1)
                ->field('id,price,original_price,sale,title,img,play_num')
                ->order('id desc')
                ->limit($limit)
                ->select();
            $arr[]=[
                'day'=>$start_time,
                'data'=>[
                    'music'=>$music,
                    'dj'=>$dj,
                    'selected_pack'=>$selectedPack,
                    'skewer'=>$skewer,
                ],

            ];
        }
        $this->getMessageCode('adminMessage.success.select', 2);
        return $arr;
    }

    function circleSt(){
        //人数
        $people_num = array_unique($this->circleModel->where('audit',1)
            ->where('status',1)->column('user_id'))[0]??0;
        //帖子shuliang
        $num =$this->circleModel->where('audit',1)
            ->where('status',1)->count();
        return ['people_num'=>$people_num,'num'=>$num];
    }

    function wechatBuildConfig(){
        return WechatSingle::getInstance()->getBuildConfig($this->request->get('url',''));
    }
}